﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ForLoopOutput:ResultBase 
    {
        private Dictionary<string,GlobalVariable> _Vars;
        /// <summary></summary>
        public Dictionary<string,GlobalVariable> Vars
        {
            get
            {
                return this._Vars;
            }
            set
            {
                this._Vars = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public ForLoopOutput():base(Guid.Empty,Guid.Empty)
        {
            this._Vars=new Dictionary<string, GlobalVariable>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="vars"></param>
        public ForLoopOutput(Guid componentID, Guid jobTicketID,Dictionary<string,GlobalVariable> vars) 
            : base(componentID, jobTicketID)
        {
            this._Vars = vars;
        }

        #region Overrides of ResultBase

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            if (this.Vars != null && this.Vars.Count > 0)
            {
                XmlNode gVarRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "GlobalVariables");
                foreach (GlobalVariable gVar in this.Vars.Values)
                {
                    XmlNode gVarNode = XmlDataUtil.AddElement(ref xDoc, gVarRootNode, "GlobalVariable");
                    XmlDataUtil.UpdateAttribute(ref xDoc, gVarNode, "VariableName", gVar.VarName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, gVarNode, "DataType", ((int)gVar.DataType).ToString());
                    XmlDataUtil.UpdateAttribute(ref xDoc, gVarNode, "VariableValue", gVar.VarValue);
                }
            }

            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                ForLoopOutput output = new ForLoopOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.Vars = new Dictionary<string, GlobalVariable>();
                XmlNodeList varNodes = xNode.SelectNodes("GlobalVariables/GlobalVariable");
                if (varNodes != null && varNodes.Count > 0)
                {
                    output.Vars = new Dictionary<string, GlobalVariable>();
                    foreach (XmlNode varNode in varNodes)
                    {
                        GlobalVariable gVar = new GlobalVariable();
                        gVar.VarName = varNode.Attributes.GetNamedItem("VariableName").Value;
                        gVar.VarValue = varNode.Attributes.GetNamedItem("VariableValue").Value;
                        gVar.DataType =
                            (SimpleDataType)int.Parse(varNode.Attributes.GetNamedItem("DataType").Value);
                        output.Vars.Add(gVar.VarName, gVar);
                    }
                }

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
